//
//  PictureViewController.m
//  XLNews
//
//  Created by 薛林 on 16/12/19.
//  Copyright © 2016年 YunTianXia. All rights reserved.
//

#import "PictureViewController.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>
#import "PictureTableViewCell.h"
#import "PictureFetchDataParameter.h"
#import "Picture.h"
#import "DataTool.h"

static NSString * const PictureCell = @"PictureCell";

@interface PictureViewController ()

@property (nonatomic, strong) NSArray *pictureArray;//图片
@property (nonatomic, copy) NSString *maxtime;

@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasic];
    [self setupTableView];
    [self setupMJRefreshHeader];

}

- (void)setupBasic {
    
    self.tableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xf0f0f0, 0x000000, 0xfafafa);
    self.navigationController.navigationBar.dk_backgroundColorPicker = DKColorPickerWithRGB(0xfa5054,0x444444,0xfa5054);
    
}
#pragma mark - 初始化tableView
- (void)setupTableView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(CGRectGetMaxY(self.navigationController.navigationBar.frame) + 10, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PictureTableViewCell class]) bundle:nil] forCellReuseIdentifier:PictureCell];
}

#pragma mark - 首次加载数据
- (void)LoadNewData {
    PictureFetchDataParameter *params = [PictureFetchDataParameter new];
    Picture *firstPicture = self.pictureArray.firstObject;
    params.recentTime = firstPicture.created_at;
    params.page = 0;
    params.maxtime = nil;
    [DataTool pictureWithParameters:params success:^(NSArray *array, NSString *maxtime) {
        [self.tableView.mj_header endRefreshing];
        self.maxtime = maxtime;
        self.pictureArray = [array mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
}


- (void)LoadMoreData {
    
    
}

#pragma mark - 初始化刷新控件
- (void)setupMJRefreshHeader {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(LoadNewData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.pictureArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PictureCell forIndexPath:indexPath];
    cell.picture = self.pictureArray[indexPath.row];
    return cell;
}

//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Picture *picture = self.pictureArray[indexPath.row];
    return picture.cellHeight;
}

@end
